<!--
数据源-接口
-->
<template>
  <div>
    <el-form v-model="dataSource" label-width="100px">
      <el-form-item label="接口来源">
        <el-radio-group v-model="form.serviceType" @change="onServiceTypeChange">
          <el-radio label="defined">预定义</el-radio>
          <el-radio label="custom">自定义</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="isCustomService" label="请求地址">
        <el-input v-if="isCustomService" v-model="form.url"></el-input>
      </el-form-item>
      <el-form-item v-else label="选择接口">
        <el-select clearable
                   v-model="form.code"
                   filterable
                   reserve-keyword
                   placeholder="请选择"
                   @change="serviceSelectChange">
          <el-option
            v-for="item in serviceList"
            :key="item.code"
            :label="item.name"
            :value="item.code">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="请求参数">
        <el-table size="mini" :data="form.paramList" border>
          <el-table-column label="键" width="150">
            <template slot-scope="scope">
              <el-input v-if="isCustomService" v-model="scope.row.code"></el-input>
              <span v-else>{{scope.row.code}}</span>

            </template>
          </el-table-column>
          <el-table-column label="值" >
            <template slot-scope="scope">
              <el-input v-model="scope.row.value">
                <el-select v-model="scope.row.type" slot="prepend" placeholder="请选择" style="width: 100px">
                  <el-option label="静态值" value=""></el-option>
                  <el-option label="动态参数" value="&"></el-option>
                </el-select>
              </el-input>
            </template>
          </el-table-column>
          <el-table-column label="" width="50">
            <template slot-scope="scope">
              <el-button type="text" @click="handleParamDel(scope.$index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-link v-if="isCustomService" type="primary" @click="handleParamAdd" :underline="false">+增加参数</el-link>

      </el-form-item>
      <!--<el-form-item label="返回结果">
        <el-input type="textarea" v-model="form.staticParams" rows="6"></el-input>
      </el-form-item>-->
      <el-form-item label="数据过滤脚本">
        <el-checkbox v-model="form.useFilter">启用</el-checkbox>
        <el-input v-if="form.useFilter" type="textarea" v-model="form.resultFilter" rows="6"></el-input>
      </el-form-item>
      <el-form-item label="刷新间隔">
        <el-checkbox v-model="form.useRefresh">启用</el-checkbox>
        <template v-if="form.useRefresh">
          <el-input-number :min="1" :step="1" v-model="form.refreshInterval">
          </el-input-number>
          单位：秒
        </template>

      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  import dataLoader from "./dataLoader";
  import serviceList from "./service";

  const defaultResultFilter = 'function _filter(data) {\n' +
    '  // do something...\n' +
    '  return data;\n' +
    '}';

  export default {
    name: 'vpd-ds-interface',
    title: '接口',
    dataLoader: dataLoader,
    props: {
      dataType: { //期望的数据类型
        type: String,
        default: 'text'
      },
      dataSource: {
        type: Object
      }
    },
    data() {
      return {
        serviceList: serviceList,
        staticParams: '{\n}',
        form: {
          serviceType: 'defined',
          code: '',
          paramList: [],
          useFilter: false,
          resultFilter: defaultResultFilter,
          useRefresh: false,
          refreshInterval: 10
        }
      }
    },
    computed: {
      isCustomService() {
        return this.form.serviceType === 'custom';
      }
    },
    beforeMount() {
      Object.assign(this.form, this.dataSource);
    },
    methods: {
      onServiceTypeChange(val) {
        this.form.code = '';
        this.form.resultFilter = defaultResultFilter;
        this.form.paramList = [];
        this.form.refreshInterval = 0;
        this.form.useFilter = false;
        this.form.useRefresh = false;
      },
      handleParamAdd() {
        this.form.paramList.push({
          type: ''
        })
      },
      handleParamDel(index) {
        this.form.paramList.splice(index, 1);
      },
      getForm() {
        return this.form;
      },
      serviceSelectChange(val) {
        if (val) {
          const service = serviceList.find(item => item.code === val);
          this.form.paramList = service.params.map(item => {
            return {
              code: item.code,
              type: '',
              value: ''
            }
          })
        } else {

        }
      },

    },

  }
</script>
